home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / BBS / SECOND_SIGHT / GEnie Cleaner.cpt / Text Utils.p < prev   
Text File  |  1991-10-18  |  4KB  |  158 lines

  1. unit TextUtils;
  2.  
  3.  
  4. interface
  5.     function AtEOF (FileRefNum: integer): boolean;
  6.     function Wr (FileRefNum: integer; TheMessage: str255): OSErr;
  7.     function WrLn (FileRefNum: integer; TheMessage: str255): OSErr;
  8.     function ReadALine (FileRefNum: integer; var TheMessage: str255): OSErr;
  9.     function RPadString (theString: str255; theLength: integer): str255;
  10.     function LPadString (theString: str255; theLength: integer): str255;
  11.     function ReplaceInString (bigString, oldString, newString: str255): str255;
  12.     function GetValue (aString: str255): longint;
  13.     function ValueToString (aNum: longint): str255;
  14.  
  15. implementation
  16.  
  17. { ------------------------------------------------------ }
  18.  
  19.     function AtEOF;    {(FileRefNum: integer): boolean}
  20.  
  21.         var
  22.             currPos, eofPos: LongInt;
  23.             myErr: OSErr;
  24.  
  25.     begin
  26.         myErr := GetFPos(FileRefNum, currPos);
  27.         if myErr = NoErr then
  28.             myErr := GetEOF(FileRefNum, eofPos);
  29.         if myErr = NoErr then
  30.             AtEOF := currPos = eofPos
  31.         else
  32.             AtEOF := true    {if there was an error, report end of file}
  33.     end;
  34.  
  35. { ------------------------------------------------------ }
  36.  
  37.     function Wr;    {(FileRefNum: integer; TheMessage: str255): OSErr}
  38.  
  39. { Function writes string to text file, returns error code           }
  40.  
  41.         var
  42.             TheLength: longint;
  43.  
  44.     begin
  45.         TheLength := length(TheMessage);
  46.         Wr := FSWrite(FileRefNum, TheLength, Pointer(ord(@TheMessage) + 1));
  47.     end;
  48.  
  49. {-----------------------------------------------------------------    }
  50.  
  51.     function WrLn;    {(FileRefNum: integer; TheMessage: str255): OSErr}
  52.  
  53.         const
  54.             ENDLINE = chr(13);
  55.  
  56.     begin
  57.         WrLn := Wr(FileRefNum, concat(TheMessage, ENDLINE))
  58.     end;
  59.  
  60. {-----------------------------------------------------------------    }
  61.  
  62.     function ReadALine;    {(FileRefNum: integer; var TheMessage: str255): OSErr}
  63.  
  64.         var
  65.             myPB: ParamBlockRec;
  66.             myString: Str255;
  67.  
  68.     begin
  69.         myString := '';
  70.         myPB.ioCompletion := nil;
  71.         myPB.ioRefNum := FileRefNum;
  72.         myPB.ioBuffer := Pointer(@myString[1]);
  73.         myPB.ioReqCount := 255;
  74.         myPB.ioPosMode := 3456; {ASCII 13*256+128}
  75.         myPB.ioPosOffset := 0; {ignored}
  76.         ReadALine := PBRead(@myPB, False);
  77.         if (myString[myPB.ioActCount] = chr(13)) then
  78.             myString[0] := char(myPB.ioActCount - 1) {Drop CR}
  79.         else
  80.             myString[0] := char(myPB.ioActCount);
  81.         TheMessage := myString
  82.     end;
  83.  
  84. {-----------------------------------------------------------------    }
  85.  
  86.     function LPadString;    {(theString: str255; theLength: integer): str255 }
  87.  
  88. {    Left justifies a string to theLength    }
  89.  
  90.     begin
  91.         if length(theString) > theLength then
  92.             theString := copy(theString, 1, theLength);
  93.         while length(theString) < theLength do
  94.             theString := concat(theString, ' ');
  95.         LPadString := theString
  96.     end;
  97.  
  98. {-----------------------------------------------------------------    }
  99.  
  100.     function RPadString;    {(theString: str255; theLength: integer): str255 }
  101.  
  102. {    Right justifies a string to theLength    }
  103.  
  104.     begin
  105.         if length(theString) > theLength then
  106.             theString := copy(theString, 1, theLength);
  107.         while length(theString) < theLength do
  108.             theString := concat(' ', theString);
  109.         RPadString := theString
  110.     end;
  111.  
  112. {-----------------------------------------------------------------    }
  113.  
  114.     function ReplaceInString; {(bigString, oldString, newString: str255): str255    }
  115.  
  116. {    Replaces oldString with newString within bigString, returns new bigString        }
  117.  
  118.         var
  119.             beginString, endString: str255;
  120.  
  121.     begin
  122.         ReplaceInString := bigString;
  123.         if (pos(oldString, bigString) > 0) then
  124.             begin
  125.                 beginString := copy(bigString, 1, pred(pos(oldString, bigString)));
  126.                 endString := copy(bigString, pos(oldString, bigString) + length(oldString), 255);
  127.                 if ((length(BeginString) + length(newString) + length(EndString)) < 255) then
  128.                     ReplaceInString := concat(beginString, newString, endString)
  129.             end
  130.     end;
  131.  
  132. {-----------------------------------------------------------------    }
  133.  
  134.     function GetValue; {(aString: str255): longint}
  135.  
  136.         var
  137.             tempLong: longint;
  138.  
  139.     begin
  140.         StringToNum(aString, tempLong);
  141.         GetValue := tempLong
  142.     end;
  143.  
  144. {-----------------------------------------------------------------    }
  145.  
  146.     function ValueToString; {(aNum: longint): str255}
  147.  
  148.         var
  149.             aString: str255;
  150.  
  151.     begin
  152.         NumToString(aNum, aString);
  153.         ValueToString := aString
  154.     end;
  155.  
  156. {-----------------------------------------------------------------    }
  157.  
  158. end.